package cn.edu.lingnan.tourism.service.Impl;

import cn.edu.lingnan.tourism.exeption.TourismException;
import cn.edu.lingnan.tourism.mapper.FavoriteMapper;
import cn.edu.lingnan.tourism.mapper.RouteMapper;
import cn.edu.lingnan.tourism.pojo.Favorite;
import cn.edu.lingnan.tourism.pojo.Route;
import cn.edu.lingnan.tourism.service.FavoriteService;
import cn.edu.lingnan.tourism.utils.TimeUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.RouteMatcher;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FavoriteServiceImpl implements FavoriteService {
    @Resource
    private FavoriteMapper favoriteMapper;
    @Transactional(rollbackFor = TourismException.class)
    @Override
    public void addFavorite(Favorite favorite) throws TourismException {
        favorite.setDate(TimeUtil.getNowTime());
        favoriteMapper.insert(favorite);
    }
    @Transactional(rollbackFor = TourismException.class)
    @Override
    public void removeFavorite(Favorite favorite) throws TourismException {
        try{
            int delete = favoriteMapper.delete(favorite);
            if(delete==0){
                throw new TourismException("删除失败");
            }
        }catch (TourismException e){
            throw new TourismException("删除失败");
        }

    }
@Transactional(readOnly = true)
    @Override
    public List<Route> getUserFavoritesRoute(Integer userId) {
        List<Route> routes = favoriteMapper.selectFavoriteRouteByUid(userId);
        return routes;
    }
@Transactional(readOnly = true)
    @Override
    public Favorite getFavoriteRouteByUidAndRid(Favorite favorite) {
        Favorite select = favoriteMapper.selectOne(favorite);
        return select;
    }
@Transactional(readOnly = true)
    @Override
    public List<Route> getFavoriteRouteHost() {
        return favoriteMapper.selectFavoriteRouteCount();
    }
}
